import celery
import multiprocessing
from logging import debug

class Paralib:
    def __init__(self):
        self.number_thread = multiprocessing.cpu_count()
        self._thread_pool=[]

    def run(self):
        """
        :return: return celery task
        """
        raise NotImplementedError("Please Implement this method")

    def start(self):
        # debug('start')
        stop = False
        while not stop:
            while len(self._thread_pool) >= self.number_thread:
                for task in self._thread_pool:
                    if task.ready():
                        result = task.wait()
                        # debug(result)
                        self._thread_pool.remove(task)
                        break
            task = self.run()
            if task is None:
                break
            self._thread_pool.append(task)
            # debug('adding task')
        for task in self._thread_pool:
            task.wait()
